package com.zian.controller.user;


import com.zian.common.R;

import com.zian.domain.dto.OrderCancelDTO;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.dto.PayOrderDTO;
import com.zian.domain.dto.SubmitOrderDTO;
import com.zian.domain.query.OrderQuery;
import com.zian.domain.vo.OrderDetailsVO;
import com.zian.service.ISysOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Api("订单管理")
@Slf4j
@RestController("SysOrderControllerUser")
@RequestMapping("/user/orders")
@RequiredArgsConstructor
public class SysOrderController {

    private final ISysOrderService orderService;

    // 提交订单
    @PostMapping("/submit")
   @ApiOperation("提交订单")
    public R<String> submitOrder(@RequestBody @Validated SubmitOrderDTO dto) {
        log.info("提交订单 参数：{}", dto);
        String orderNo = orderService.submitOrder(dto);
        return R.ok(orderNo);
    }



    @PutMapping("/cancel")
   @ApiOperation("取消订单")
    public R cancelOrder(@RequestBody OrderCancelDTO dto) {
        log.info("取消订单:{}", dto);
        orderService.cancelOrder(dto, false);
        return R.ok();
    }

    // 完成交易
    @PutMapping("/complete/{id}")
   @ApiOperation("完成交易")
    public R completeOrder(@PathVariable Long id) {
        log.info("完成交易:{}", id);
        orderService.completeOrder(id);
        return R.ok();
    }

    // 查询自己的订单列表
    @GetMapping("/page")
   @ApiOperation("查询订单列表")
    public R<PageDTO<OrderDetailsVO>> queryCurUserOrderList(OrderQuery query) {
        log.info("查询参数：{}", query);
        PageDTO<OrderDetailsVO> vos = orderService.queryCurUserOrderList(query);
        return R.ok(vos);
    }

    @GetMapping("/{orderNo}")
   @ApiOperation("获取订单详情")
    public R<OrderDetailsVO> getOrderDetail(@PathVariable String orderNo) {
        OrderDetailsVO vo = orderService.getOrderDetail(orderNo);
        return R.ok(vo);
    }

}
